#include "StdAfx.h"
#include "DrawView.h"
namespace UILib
{
    CDrawView::CDrawView(void)  : uID_( -1 )\
        , bVisible_(true)\
        , bEnable_(true)\
        , pListener_(NULL)
        , eStatus_(Elem_Normal)\
        , bLBDown_(false)
        , bNeedInvalidate_(false)\
        , pTipsListenser_(NULL)\
        , bShowTips_(false)\
        , bCapture_(false)\
        , bFocused_(false)
    {
        memset(&UIRect_, 0, sizeof(UIRect) );
    }

    CDrawView::CDrawView( const PUIRect pRect, const UINT uID /*= -1*/, ViewListener* pListener /*= NULL */ ) : uID_( uID )\
        , bVisible_(true)\
        , bEnable_(true)\
        , pListener_(pListener)
        , eStatus_(Elem_Normal)\
        , bLBDown_(false)
        , bNeedInvalidate_(false)\
        , pTipsListenser_(NULL)\
        , bShowTips_(false)\
        , bCapture_(false)\
        , bFocused_(false)
    {
        if ( pRect )
        {
            memcpy( &UIRect_, pRect, sizeof(UIRect) );
        }
        else
        {
            memset(&UIRect_, 0, sizeof(UIRect) );
        }
    }
    CDrawView::~CDrawView(void)
    {
    }

    void CDrawView::SetID( const UINT uID )
    {
        uID_ = uID;
    }

    void CDrawView::SetPosition( const POINT pt )
    {
        UIRect_.SetPosition(pt);
    }

    void CDrawView::SetSize( const SIZE sz )
    {
        UIRect_.SetSize(sz);
    }

    void CDrawView::SetRect( const PUIRect pRect )
    {
        memcpy( &UIRect_, pRect, sizeof(UIRect) );
    }
    void CDrawView::GetRECT( RECT& rc )
    {
        PUIRect pUIRect = CDrawView::GetRect();
        RECT rcEmem = { 0 };
        if ( pUIRect )
        {
            pUIRect->GetRECT( rcEmem );
        }
        memcpy(&rc, &rcEmem, sizeof(RECT));

    }
    void CDrawView::SetVisible( const bool bVisible )
    {
        bVisible_ = bVisible;
    }

    void CDrawView::SetEnable( const bool bEnable /*= true*/ )
    {
        bEnable_ = bEnable;
    }

    void CDrawView::SetListener( ViewListener* pListener )
    {
        pListener_ = pListener;
    }

    void CDrawView::HandleMessage( UINT uMsg, WPARAM wParam, LPARAM lParam, HWND hWnd, BOOL& bHandled )
    {
        if (!bVisible_ || !bEnable_ )
        {
            return;
        }

        RECT rcElem = { 0 };
        UIRect_.GetRECT(rcElem);

        bool bInElem =  HitCursor( lParam, rcElem );
        switch( uMsg )
        {
        case WM_MOUSEMOVE :
            {
                POINT pt = {rcElem.right + 10, rcElem.bottom + 10};
                if (bInElem)
                {
                    if ( eStatus_ != Elem_Hover && !bLBDown_ )
                    {
                        eStatus_ = Elem_Hover;
                        bNeedInvalidate_ = true;

                        if ( !wszTips_.empty() && bShowTips_ && pTipsListenser_ )
                        {
                            pTipsListenser_->OnShowTips(wszTips_, pt, rcElem );
                        }
                    }
                }
                else if ( !bLBDown_ )
                {
                    if (  eStatus_ != Elem_Normal )
                    {
                        eStatus_ = Elem_Normal;
                        bNeedInvalidate_ = true;

                        if (  bShowTips_ && pTipsListenser_ )
                        {
                            pTipsListenser_->OnShowTips(L"", pt, rcElem );
                        }
                    }
                }
            }
            break;
        case WM_MOUSELEAVE :
            {
                if ( eStatus_ != Elem_Normal )
                {
                    eStatus_ = Elem_Normal;
                    bNeedInvalidate_ = true;
                    bLBDown_ = false;
                    POINT pt = {rcElem.right + 10, rcElem.bottom + 10};
                    if (  bShowTips_ && pTipsListenser_ )
                    {
                        pTipsListenser_->OnShowTips(L"", pt, rcElem );
                    }
                    ::ReleaseCapture();
                }
            }	
            break;
        case WM_LBUTTONDOWN :
            {
                if (  bInElem  )
                {
                    if ( eStatus_ != Elem_Down )
                    {
                        eStatus_ = Elem_Down;
                        bNeedInvalidate_ = true;
                        ::SetCapture(hWnd);
                    }
                    bLBDown_ = true;
                }
            }
            break;
        case WM_LBUTTONUP :
            {
                bool bLBDown = bLBDown_;
                if (bLBDown_)
                {
                    bLBDown_ = false;
                    bNeedInvalidate_ = true;
                    if (  !bInElem  )
                    {
                        eStatus_ = Elem_Normal;
                    }
                    else
                    {
                        eStatus_ = Elem_Hover;
                    }
                    ::ReleaseCapture();
                }

                if (bInElem)
                {
                    eStatus_ = Elem_Hover;
                    bNeedInvalidate_ = true;

                    if ( pListener_ && bLBDown )
                    {
                        pListener_->OnClick(uID_);
                    }
                }
                else
                {
                    eStatus_ = Elem_Normal;
                }
            }
            break;
        }
    }

    bool CDrawView::HitCursor( DWORD dwPT, RECT& rc )
    {
        POINT pt = {GET_X_LPARAM(dwPT), GET_Y_LPARAM(dwPT)};
        return HitCursor(pt, rc);
    }

    bool CDrawView::HitCursor( POINT ptMouse, RECT& rc )
    {
        return PtInRect( &rc, ptMouse ) ? true : false;
    }

    bool CDrawView::HitDraw( const RECT &rcPaint, const RECT& rc )
    {
        RECT rectTemp = {0};
        return ::IntersectRect(&rectTemp, &rcPaint, &rc) ? true : false;
    }

    void CDrawView::SetTipsListener( TipsListenser* pTipsListener )
    {
        pTipsListenser_ = pTipsListener;
    }

    void CDrawView::SetTips( wstring wszTips, bool bShow /*= true */ )
    {
        wszTips_ = wszTips;
        bShowTips_ = bShow;
    }

    bool CDrawView::NeedInvalidata( RECT& rcReDraw )
    {
        UIRect_.GetRECT(rcReDraw);
        return bNeedInvalidate_;
    }

    void CDrawView::SetCapture( bool bCapture /*= false */ )
    {
        bCapture_ = bCapture;
    }

    void CDrawView::SetFocus()
    {
        bFocused_ = true;
    }
}